//
//  LGLSearchBar.m
//  LGLSearchBar-Dome
//
//  Created by 李国良 on 2016/9/30.
//  Copyright © 2016年 李国良. All rights reserved.
//

#import "LGLSearchBar.h"
#import <QuartzCore/QuartzCore.h>
@interface LGLSearchBar ()<UISearchBarDelegate>

{
    UISearchBar * _searchBar;
}


@end

@implementation LGLSearchBar

/*
     还需要添加 修改放大镜的图片
 
 
 */

- (instancetype)initWithFrame:(CGRect)frame  searchBarStyle:(LGLSearchBarStyle)style {
    self = [super initWithFrame:frame];
    if (self) {
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _searchBar.barStyle =  (style == LGLSearchBarStyleDefault) ? UISearchBarStyleDefault : ((style == LGLSearchBarStyleProminent) ? UISearchBarStyleProminent : UISearchBarStyleMinimal);
//   _searchBar.barStyle = UISearchBarStyleMinimal;
        _searchBar.placeholder = @"请输入搜索关键字";
//        _searchBar.placeholder = NSLocalizedString(@"请输入搜索内容", nil);

        _searchBar.barTintColor = [UIColor whiteColor];
// _searchBar.barTintColor =[UIColor colorWithHexString:@"efeff4"];
// _searchBar.tintColor =[UIColor colorWithHexString:@"efeff4"];
_searchBar.layer.borderWidth = 1;_searchBar.layer.borderColor = [[UIColor whiteColor] CGColor];

//        _searchBar.backgroundColor = [UIColor yellowColor];
//        UITextField * texFeiled = [self searchBarTextFeild];
//_searchBar.backgroundColor = [UIColor yellowColor];
//        _searchBar.searchTextField.layer.borderWidth = 0.1;
//              _searchBar.searchTextField.layer.borderColor = [UIColor clearColor].CGColor;
//
//              _searchBar.searchTextField.layer.masksToBounds = YES;
//              _searchBar.searchTextField.layer.cornerRadius = 0;
                    UIImage *image = [UIImage imageNamed: @"search"];


  UIImageView *iView = [[UIImageView alloc] initWithImage:image];

            iView.frame = CGRectMake(0, 10, 20 , 20);

            UIView *myview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40 , 40)];

        [myview addSubview:iView];
//            [self searchBarTextFeild].leftView = nil;

            [self searchBarTextFeild].leftView = myview;

        /*
         UITextAutocapitalizationTypeNone,             除非自己点击大写,否则永不大写
         UITextAutocapitalizationTypeWords,            以单词来区分,每个单词首字母大写
         UITextAutocapitalizationTypeSentences,        以句子来区分
         UITextAutocapitalizationTypeAllCharacters,    所有字母全部大写
         */
        _searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
        //这个是透视属性
//        _searchBar.translucent = YES;
        _searchBar.delegate = self;
        [self addSubview:_searchBar];
    }
    return self;
}

#pragma mark ====  UITextFeildDElegate =======
// return NO to not resign first responder
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
        searchBar.showsCancelButton = NO;

    return YES;
}

// called when keyboard search button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    self.block(searchBar.text);
    searchBar.text = nil;
    [searchBar resignFirstResponder];
            searchBar.showsCancelButton = NO;
}

 // called when cancel button pressed
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    self.block(searchBar.text);
    searchBar.text = nil;
    [searchBar resignFirstResponder];
        searchBar.showsCancelButton = NO;

}

// 重新设置 searchbar cancel按钮 为搜索
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    searchBar.showsCancelButton = YES;
    for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            UIButton * cancel =(UIButton *)view;
                 UIColor *textColor = self.ViewController.view.tintColor;

//            [cancel setBackgroundColor:textColor];
            [cancel setTitle:@"搜索" forState:UIControlStateNormal];
//            [cancel setTitle:NSLocalizedString(@"搜索", nil) forState:UIControlStateNormal];

            cancel.titleLabel.font = [UIFont systemFontOfSize:14];
        }
    }
}

                   // return NO to not resign first responder

#pragma mark ==== 重写set方法 ===============

- (void)setPlaceholderFontSize:(CGFloat )placeholderFontSize {
    _placeholderFontSize = placeholderFontSize;
//    UISearchBar *searchBar = [[UISearchBar alloc]init];
      self. placeholderFontSize =_placeholderFontSize;
//     [[self searchBarTextFeild] setValue:[UIFont systemFontOfSize:placeholderFontSize] forKeyPath:@"_placeholderLabel.font"];
}

- (void)setTextFontSize:(CGFloat)textFontSize {
    _textFontSize = textFontSize;
    [self searchBarTextFeild].font = [UIFont systemFontOfSize:textFontSize];
}

- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    _searchBar.placeholder = placeholder;
}

- (void)setTintColor:(UIColor *)tintColor {
    _tintColor = tintColor;
    _searchBar.tintColor = tintColor;
}

- (void)setBarBackgroudColor:(UIColor *)barBackgroudColor {
    _barBackgroudColor = barBackgroudColor;
     _searchBar.barTintColor = _barBackgroudColor;
}

- (void)setTextBackgroudColor:(UIColor *)textBackgroudColor {
    _textBackgroudColor = textBackgroudColor;
    [[[_searchBar.subviews firstObject]subviews]lastObject].backgroundColor = textBackgroudColor;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    _placeholderColor = placeholderColor;
//    [[self searchBarTextFeild] setValue:placeholderColor forKeyPath:@"_placeholderLabel.textColor"];
    self.searchBarTextFeild.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@""attributes:@{NSForegroundColorAttributeName:@"_placeholderLabel.textColor"}];
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    [self searchBarTextFeild].textColor = textColor;
}

- (void)setSearchBarImage:(NSString *)imageName scale:(CGFloat)scale {
    UIImageView * leftView = [[UIImageView alloc] initWithImage:[self changeImageSize:imageName scale:scale]];
    [self searchBarTextFeild].leftView = leftView;
}

- (void)setTextBordStyle:(LGLTextBorderStyle)textBordStyle {
    _textBordStyle = textBordStyle;
    UITextBorderStyle bordStyle = (textBordStyle == LGLTextBorderStyleNone) ? UITextBorderStyleNone : ((textBordStyle == LGLTextBorderStyleLine) ? UITextBorderStyleLine : ((textBordStyle == LGLTextBorderStyleBezel) ? UITextBorderStyleBezel : UITextBorderStyleRoundedRect));
    [self searchBarTextFeild].borderStyle = bordStyle;
}

- (void)setSearchBarBordeWidth:(CGFloat)Width bordColor:(UIColor *)bordColor bordRadius:(CGFloat)bordcornerRadius {
//    UITextField * texFeiled = _searchBar.searchTextField;
//    texFeiled.layer.masksToBounds = YES;
//
//    texFeiled.layer.borderWidth = Width;
//    texFeiled.layer.borderColor = [UIColor redColor].CGColor;
//
//    texFeiled.layer.cornerRadius = bordcornerRadius;
//    texFeiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"输入"attributes:@{NSForegroundColorAttributeName: [UIColor red]}];

    self.searchBarTextFeild.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@""attributes:@{NSForegroundColorAttributeName:@"_placeholderLabel.textColor"}];


}

- (void)searchBarTextSearchTextBlock:(SearchTextBlock)block {
    self.block = block;
}

// 修改图片的大小
-  (UIImage *)changeImageSize:(NSString *)imageName scale:(CGFloat)scale
{
    NSData *imgData = UIImagePNGRepresentation([UIImage imageNamed:imageName]);
    UIImage * image = [UIImage imageWithData:imgData scale:scale];
    //声明使用自定义的图片
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return image;
}
//获取输入框
- (UITextField *)searchBarTextFeild {
//    UITextField * texFeild = nil;
//    for (UIView* subview in [[_searchBar.subviews lastObject] subviews]) {
////        subview.backgroundColor = [UIColor whiteColor];
////                             subview.layer.contents = nil;
//        if ([subview isKindOfClass:[UITextField class]]) {
//            texFeild = (UITextField*)subview;
//            subview.backgroundColor = [UIColor whiteColor];
//           subview.layer.contents = nil;
//        }
//        else if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
//        {
//
//            [subview removeFromSuperview];
//        }
//    }
UITextField *textField = (UITextField*)[self findSubview:@"UITextField" resursion:YES];

    return textField;
}

@end
